


<div class="panel panel-default">
	<div class="panel-heading">
		<div class="row">
			<b class="col-xs-6">PurchaseJournalItem</b>
			<div class="col-xs-6 text-right">
				<a id="btn-addrowpurchasejournalitem" class="btn-sm btn btn-primary" href="javascript:void(0);"><i class="glyphicon glyphicon-plus"></i> Add Item</a>			
			</div>
		</div>
	</div>
	<div id="purchasejournalitem-list" class="panel-body">
		<?php echo $this->renderPartial('application.views.purchaseJournalItem._tabularform_additional', array('modelPurchaseJournalItem'=>$modelPurchaseJournalItem)); ?>
	</div>
</div>

<script>
	$('#purchasejournalitem-list').on('click', '.input-group.date', function(){$('.input-group.date').datepicker({
		format: "dd/mm/yyyy",
		keyboardNavigation: false,
		autoclose: true,
		todayHighlight: true,
	});});
</script>

<?php
Yii::app()->clientScript->registerScript('addrowpurchasejournalitem', "
	$('#btn-addrowpurchasejournalitem').click(function(){
		$.ajax({
			url: '".Yii::app()->urlManager->createUrl('purchasejournalitem/formaddrow') . "',
			type: 'post',
			data: $('#" . $form->id . "').serialize(),
			dataType: 'html',
			success: function(data) {
				$('#purchasejournalitem-list').html(data);
			},
			error: function() {
				alert('An error has occured while adding a new block.');
			}
		});
	});
");
?>

<?php
Yii::app()->clientScript->registerScript('deleterowpurchasejournalitem',"
	$('#purchasejournalitem-list').on('click', '.btn-deleterowpurchasejournalitem', function(){
		rowIndex = $(this).closest('tr').attr('rowIndex');
		$.ajax({
			url: '".Yii::app()->urlManager->createUrl('purchasejournalitem/formdeleterow')."',
			type: 'post',
			data: $('#".$form->id."').serialize()+'&rowIndex='+rowIndex,
			dataType: 'html',
			success: function(data) {
				$('#purchasejournalitem-list').html(data);
			},
			error: function() {
				alert('An error has occured while deleting a row.');
			}
		});
	});
");
?>

<?php
Yii::app()->clientScript->registerScript('calcamountrowpurchasejournalitem', "
	$('#purchasejournalitem-list').on('change', '.PurchaseJournalItem_rate', function(){
		// console.log($(this).closest('tr').attr('rowIndex'));
		rowIndex = $(this).closest('tr').attr('rowIndex');
		rate = $('#PurchaseJournalItem_'+rowIndex+'_totalWeight').val();
		totalWeight = $('#PurchaseJournalItem_'+rowIndex+'_rate').val();
		$('#PurchaseJournalItem_'+rowIndex+'_amount').val((rate * totalWeight).toFixed(2));
		$('.PurchaseJournalItem_amount').trigger('change');
	});
	$('#purchasejournalitem-list').on('change', '.PurchaseJournalItem_totalWeight', function(){
		// console.log($(this).closest('tr').attr('rowIndex'));
		rowIndex = $(this).closest('tr').attr('rowIndex');
		rate = $('#PurchaseJournalItem_'+rowIndex+'_totalWeight').val();
		totalWeight = $('#PurchaseJournalItem_'+rowIndex+'_rate').val();
		$('#PurchaseJournalItem_'+rowIndex+'_amount').val((rate * totalWeight).toFixed(2));
		$('.PurchaseJournalItem_amount').trigger('change');
	});
	$('#purchasejournalitem-list').on('change', '.PurchaseJournalItem_amount', function(){
		$(this).val(parseFloatSafe($(this).val()).toFixed(2));
		totalAmount = 0;
		$('.PurchaseJournalItem_amount').each(function(item){
			// console.log($(this).val());
			totalAmount += parseFloatSafe($(this).val());
		});
		$('#totalAmount').html(totalAmount.toFixed(2));
	});
	
");
?>